package com.gdth.bank.presenter;

import java.util.Date;
import java.util.List;

import com.gdth.bank.event.CjkhjlListEvent;
import com.gdth.bank.model.CjkhjlModel;
import com.gdth.bank.view.CjkhjlListView;
import com.gdth.database.bean.TAppBfrjJbxxb;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 拜访日志查询列表
 * @author arvin
 *
 */
public class CjkhjlListPresenter {
	private CjkhjlListView mCjkhjlView;
	private CjkhjlModel mCjkhjlModel;
	
	public int loadCjkhhl = 101;
	
	public CjkhjlListPresenter(CjkhjlListView mCjkhjlView){
		this.mCjkhjlView = mCjkhjlView;
		mCjkhjlModel = new CjkhjlModel();
	}

	public void getqureAllCjkhxx(String zh, String val, Date date, final int type){
		mCjkhjlModel.getqureAllCjkhxx(zh, val, date)
				.subscribeOn(Schedulers.io())
				.unsubscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Subscriber<List<TAppBfrjJbxxb>>(){

					@Override
					public void onCompleted() {
						// TODO Auto-generated method stub
						mCjkhjlView.hideProgressDialog();
					}

					@Override
					public void onError(Throwable arg0) {
						// TODO Auto-generated method stub
						mCjkhjlView.showMessage("获取失败");
					}

					@Override
					public void onNext(List<TAppBfrjJbxxb> arg0) {
						// TODO Auto-generated method stub
						mCjkhjlView.refreshData(arg0);
//						if (type == 1) {
//
//						} else
//							mCjkhjlView.loadMoreData(arg0);
					}
				}
				);
	}

	public void getAllCjkhxx(String zh, String val, Date date, final int type){
		mCjkhjlModel.getAllCjkhxx(zh, val, date)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<TAppBfrjJbxxb>>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
				mCjkhjlView.hideProgressDialog();
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mCjkhjlView.showMessage("获取失败");
			}

			@Override
			public void onNext(List<TAppBfrjJbxxb> arg0) {
				// TODO Auto-generated method stub
				if (type == 1) {
					mCjkhjlView.refreshData(arg0);
				}
//				} else
//					mCjkhjlView.loadMoreData(arg0);
			}});
	}

	public void delete(String wybs) {
		// TODO Auto-generated method stub
		mCjkhjlModel.delCjkhJbxxZt(wybs)
			.subscribeOn(Schedulers.io())
			.unsubscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Subscriber<String>(){

				@Override
				public void onCompleted() {
					// TODO Auto-generated method stub
					mCjkhjlView.hideProgressDialog();
				}
	
				@Override
				public void onError(Throwable arg0) {
					// TODO Auto-generated method stub
					mCjkhjlView.showMessage("失败");
				}
	
				@Override
				public void onNext(String arg0) {
					// TODO Auto-generated method stub
					mCjkhjlView.deleteData(arg0);
				}
			});
	}

	public void delCjkhJbxxZt(String uuid,String cjrsfz) {
		JSONObject val=new JSONObject();
		try {
			val.put("cjrsfz", cjrsfz);
			val.put("uuid", uuid);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		mCjkhjlModel.delBfrjInfo(val.toString())
				.subscribeOn(Schedulers.io())
				.unsubscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Subscriber<String>(){

					@Override
					public void onCompleted() {
						mCjkhjlView.hideProgressDialog();
					}

					@Override
					public void onError(Throwable arg0) {
						mCjkhjlView.showMessage("删除失败");
					}

					@Override
					public void onNext(String arg0) {
						if(arg0.equals("1")){
							EventBus.getDefault().post(new CjkhjlListEvent(CjkhjlListEvent.BFLB_DEL_NET_SUC,uuid));
						}else{

						}

					}
				});
	}

}
